package com.imti.mall.product.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.pms.pojo.PmsProductAttribute;
import com.imti.mall.model.pms.pojo.PmsProductAttributeCategory;
import com.imti.mall.model.pms.vo.ProductAttrCate;
import com.imti.mall.product.service.PmsProductAttributeCategoryService;
import com.imti.mall.product.service.PmsProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductAttributeCategoryController
 * Description:
 * date: 2022/9/13 14:40
 *
 * @author: youhun
 * @version:
 * @since JDK 1.8
 */
@RestController
@Api(tags = "商品属性分类管理")
@RequestMapping("/api/v1/product/attribute/category")
public class PmsProductAttributeCategoryController {
    
    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    @ApiOperation("添加商品属性分类数据")
    @PostMapping(value = "/addPmsProductAttributeCategory")
    public CommonResult addPmsProductAttributeCategory(PmsProductAttributeCategory pmsProductAttributeCategory) {
        boolean req = pmsProductAttributeCategoryService.addPmsProductAttributeCategory(pmsProductAttributeCategory);
        if(req){
            return CommonResult.success("添加成功");
        }else {
            return CommonResult.failed("添加失败");
        }

    }

    @ApiOperation("根据ID修改商品属性分类数据")
    @PostMapping (value = "/updatePmsProductAttributeCategoryById")
    public CommonResult updatePmsProductAttributeCategory(PmsProductAttributeCategory pmsProductAttributeCategory) {
        boolean req = pmsProductAttributeCategoryService.updatePmsProductAttributeCategory(pmsProductAttributeCategory);
        if(req){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }

    }
    /**
     * 根据ID删除商品分类数据
     * @param Id
     * @return
     */
    @ApiOperation("根据ID删除商品属性分类数据")
    @GetMapping(value = "/deletePmsProductAttributeCategoryById/{Id}")
    public CommonResult deletePmsProductAttributeCategoryById(@PathVariable Long Id) {
        PmsProductAttributeCategory pmsProductAttributeCategory=new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setId(Id);
        boolean req = pmsProductAttributeCategoryService.deletePmsProductAttributeCategory(pmsProductAttributeCategory);
        if(req){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }

    }


    /**
     * 查询商品分类列表
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询商品分类")
    @PostMapping(value = "/findProductAttributeCategoryById" )
    public CommonResult findProductAttributeCategoryById
    (@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Map<String,Object> map=new HashMap<>();
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.findPmsProductAttributeCategory(pageSize, pageNum);
        long total =pmsProductAttributeCategoryService.getPmsProductAttributeCategory();
        map.put("list" ,CommonPage.restPage(list));
        map.put("total",total);
        return CommonResult.success(map);
    }


    /**
     * 查询商品分类用于选中分类搜索时筛选属性
     * @return
     */
    @ApiOperation(value = "查询商品分类用于选中分类搜索时筛选属性（app调用商品商品分类）")
    @PostMapping(value = "/findProductAttributeCategoryAndProductAttribute" )
    public CommonResult findProductAttributeCategoryAndProductAttribute() {
        List <ProductAttrCate> productAttrCateList=pmsProductAttributeCategoryService.findPmsProductAttributeCategoryToID();
        for(int i=0;i<productAttrCateList.size();i++){
            PmsProductAttribute pmsProductAttribute=new PmsProductAttribute();
            //查询属性表
            pmsProductAttribute.setProductAttributeCategoryId(productAttrCateList.get(i).getId());
            pmsProductAttribute.setType(0);
            productAttrCateList.get(i).setProductAttributeList(pmsProductAttributeService.findPmsProductAttributeByType(pmsProductAttribute));
        }
        return CommonResult.success(productAttrCateList);
    }





}
